﻿namespace Hsenl {
    public partial struct HTask {
        // 返回一个空壳子, 在Completed判断时, 总是返回true
        public static HTask Completed => new(null, HTaskType.Create);

        internal static HTask Builder() {
            return new HTask(HTaskBodyPool.RentBody<HTaskCompletionBody>(), HTaskType.Builder);
        }

        internal static HTask Builder<TBody>() where TBody : IHTaskBody, new() {
            return new HTask(HTaskBodyPool.RentBody<TBody>(), HTaskType.Builder);
        }

        public static HTask Create() {
            return new HTask(HTaskBodyPool.RentBody<HTaskCompletionBody>(), HTaskType.Create);
        }

        public static HTask Create<TBody>() where TBody : IHTaskBody, new() {
            return new HTask(HTaskBodyPool.RentBody<TBody>(), HTaskType.Create);
        }

        public static SwitchToMainThreadPoolAwaitable SwitchToMainThread() => SwitchToMainThreadPoolAwaitable.Create();
        
        public static SwitchToThreadPoolAwaitable SwitchToThreadPool() => SwitchToThreadPoolAwaitable.Create();
    }

    public partial struct HTask<T> {
        internal static HTask<T> Builder() {
            return new HTask<T>(HTaskBodyPool.RentBody<HTaskCompletionBody<T>>(), HTaskType.Builder);
        }

        internal static HTask<T> Builder<TBody>() where TBody : IHTaskBody<T>, new() {
            return new HTask<T>(HTaskBodyPool.RentBody<TBody>(), HTaskType.Builder);
        }

        public static HTask<T> Create() {
            return new HTask<T>(HTaskBodyPool.RentBody<HTaskCompletionBody<T>>(), HTaskType.Create);
        }

        public static HTask<T> Create<TBody>() where TBody : IHTaskBody<T>, new() {
            return new HTask<T>(HTaskBodyPool.RentBody<TBody>(), HTaskType.Create);
        }
    }
}